package com.activities.api.data;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.activities.api.data.providers.ActivityStore;
import com.activities.api.data.providers.ActivityStoreImpl;
import com.activities.api.data.providers.GroupStore;
import com.activities.api.data.providers.GroupStoreImpl;
import com.activities.api.data.providers.ParticipantStore;
import com.activities.api.data.providers.ParticipantStoreImpl;
import com.activities.api.data.providers.UserStore;
import com.activities.api.data.providers.UserStoreImpl;
import com.activities.api.data.server.ActivityService;
import com.activities.api.data.server.ActivityServiceImpl;
import com.activities.api.data.server.GroupService;
import com.activities.api.data.server.GroupServiceImpl;
import com.activities.api.data.server.ParticipantService;
import com.activities.api.data.server.ParticipantServiceImpl;
import com.activities.api.data.server.UserService;
import com.activities.api.data.server.UserServiceImpl;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

public class GuiceConfig extends GuiceServletContextListener {

	@Override
	protected Injector getInjector() {
		return Guice.createInjector(new ServletModule() {
			@Override
			protected void configureServlets() {
				bind(UserService.class).to(UserServiceImpl.class);
				bind(UserStore.class).to(UserStoreImpl.class);

				bind(ActivityService.class).to(ActivityServiceImpl.class);
				bind(ActivityStore.class).to(ActivityStoreImpl.class);

				bind(GroupService.class).to(GroupServiceImpl.class);
				bind(GroupStore.class).to(GroupStoreImpl.class);

				bind(ParticipantStore.class).to(ParticipantStoreImpl.class);
				bind(ParticipantService.class).to(ParticipantServiceImpl.class);
				
				EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("transactions-optional");
				bind(EntityManagerFactory.class).toInstance(entityManagerFactory);
				
				Map<String, String> params = new HashMap<String, String>();
                params.put("com.sun.jersey.config.property.packages", "com.activities.api.data.server"); 
				serve("/*").with(GuiceContainer.class, params);
			}
		});
	}

}
